package ${package_name}.controller;
import com.evada.inno.core.annotation.Rest;
import ${package_name}.service.I${table_name}Service;
import ${package_name}.model.${table_name};
import ${package_name}.dto.${table_name}DTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.http.MediaType;
import com.evada.inno.common.domain.ResultData;
import com.evada.inno.core.util.AssertUtils;
import com.runoqd.itc.common.controller.BaseController;
import org.springframework.web.bind.annotation.*;
import com.runoqd.itc.common.aspect.RequireToken;
import com.runoqd.itc.base.ResponseData;
import com.github.pagehelper.PageInfo;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * ${table_annotation}controller
 *
 * @author ${author}
 * @date ${date}
 */

@RestController
@CrossOrigin
@RequestMapping(value = "/${table_name}")
public class ${table_name}Controller extends BaseController{

    private static final Logger logger = LoggerFactory.getLogger(${table_name}Controller.class);

    @Autowired
    private I${table_name}Service ${table_name?uncap_first}Service;

    /**
     *
     * 根据ID查询${table_annotation}详情
     *
     * @param id  ${table_annotation}id
     * @return
     */
    @RequireToken
    @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResponseData findById(@PathVariable("id") String id)throws Exception {

        if(StringUtils.isBlank(id)){
            ResponseData responseData = new ResponseData("500", "查询${table_annotation}详情失败，请求参数不合法");
            return responseData;
        }

        try {
            ${table_name} info = ${table_name?uncap_first}Service.selectByPrimaryKey(id);
            if(info == null){
                ResponseData responseData = new ResponseData("500", "查询${table_annotation}详情失败");
                return responseData;
            }
            ResponseData responseData = ResponseContents.ok();
            responseData.addData("info",info);
            return responseData;
        } catch (Exception e) {
            logger.error("查询${table_annotation}详情失败:" + e.getMessage());
            ResponseData responseData = new ResponseData("500", "查询${table_annotation}详情失败，" + e.getMessage());
            return responseData;
        }
    }


    /**
     *
     * 查询${table_annotation}列表
     *
     * @param request
     * @param query
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequireToken
    public ResponseData list(HttpServletRequest request, @RequestBody ${table_name}Query query) {

        try {

            PageInfo<${table_name}VO> result = ${table_name?uncap_first}Service.findPage(query);

            ResponseData responseData = ResponseContents.ok();
            responseData.addData("page",result);
            return responseData;
        } catch (Exception e) {
            logger.error("查询${table_annotation}列表失败:" + e.getMessage());
            ResponseData responseData = new ResponseData("500", "查询${table_annotation}列表失败，" + e.getMessage());
            return responseData;
        }
    }



    /**
     *
     * 新增${table_annotation}
     *
     * @param ${table_name?uncap_first}  ${table_annotation}
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequireToken
    public ResponseData create(@RequestBody ${table_name} ${table_name?uncap_first}) throws Exception {

        // TODO 参数校验

        try {

            int result = ${table_name?uncap_first}Service.insertSelective(${table_name?uncap_first});

            if(result <=0){
                logger.error("新增${table_annotation}失败:" + e.getMessage());
                ResponseData responseData = new ResponseData("500", "新增${table_annotation}失败");
                return responseData;
            }

            ResponseData responseData = ResponseContents.ok();
            return responseData;
        } catch (Exception e) {
            logger.error("新增${table_annotation}失败:" + e.getMessage());
            ResponseData responseData = new ResponseData("500", "新增${table_annotation}失败，" + e.getMessage());
            return responseData;
        }
    }

    /**
     *
     * 删除${table_annotation}
     *
     * @param ${table_name?uncap_first}  ${table_annotation}
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequireToken
    public ResponseData delete(@PathVariable("id") String id) throws Exception {

        if(StringUtils.isBlank(id)){
            ResponseData responseData = new ResponseData("500", "查询${table_annotation}详情失败，请求参数不合法");
            return responseData;
        }

        try {

            int result = ${table_name?uncap_first}Service.deleteByPrimaryKey(id);

        if(result <=0){
            logger.error("删除${table_annotation}失败:" + e.getMessage());
            ResponseData responseData = new ResponseData("500", "删除${table_annotation}失败");
            return responseData;
        }

        ResponseData responseData = ResponseContents.ok();
        return responseData;
        } catch (Exception e) {
            logger.error("删除${table_annotation}失败:" + e.getMessage());
            ResponseData responseData = new ResponseData("500", "删除${table_annotation}失败，" + e.getMessage());
            return responseData;
        }
    }



    /**
     *
     * 更新${table_annotation}
     *
     * @param id ${table_annotation}id
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @RequireToken
    public ResponseData update${table_name}(@PathVariable("id") String id,@RequestBody ${table_name} ${table_name?uncap_first}) throws Exception {
        if(StringUtils.isBlank(id)){
            ResponseData responseData = new ResponseData("500", "更新${table_annotation}失败，请求参数不合法");
            return responseData;
        }

        try {
            ${table_name?uncap_first}.setId(id);
            int result = ${table_name?uncap_first}Service.updateByPrimaryKeySelective(${table_name?uncap_first});

            if(result <=0){
                logger.error("更新${table_annotation}失败:" + e.getMessage());
                ResponseData responseData = new ResponseData("500", "更新${table_annotation}失败");
                return responseData;
            }

            ResponseData responseData = ResponseContents.ok();
            return responseData;
        } catch (Exception e) {
            logger.error("更新${table_annotation}失败:" + e.getMessage());
            ResponseData responseData = new ResponseData("500", "更新${table_annotation}失败，" + e.getMessage());
            return responseData;
        }
    }

}